package com.sc.nft.admin.controller;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sc.nft.admin.request.OrderDetailExportRequest;
import com.sc.nft.admin.request.OrderExportRequest;
import com.sc.nft.admin.request.OrderPageRequest;
import com.sc.nft.entity.vo.OrderDetailExportVO;
import com.sc.nft.entity.vo.OrderDetailListVO;
import com.sc.nft.entity.vo.OrderExportVO;
import com.sc.nft.entity.vo.OrderSimpleVO;
import com.sc.nft.admin.vo.OrderVO;
import com.sc.nft.annotation.SysMemberLogin;
import com.sc.nft.dto.OrderDTO;
import com.sc.nft.entity.Order;
import com.sc.nft.entity.dto.AdminSecondOrderNowDataLockVO;
import com.sc.nft.entity.dto.UnfreezeDTO;
import com.sc.nft.exception.GlobalException;
import com.sc.nft.service.OrderService;
import com.sc.nft.service.SecondOrderService;
import com.sc.nft.sup.PageResult;
import com.sc.nft.sup.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;

@Slf4j
@RequiredArgsConstructor
@RestController
@RequestMapping("/admin/order")
@Api(tags = "后台订单模块")
public class OrderAdminController {

    private final OrderService orderService;

    private final SecondOrderService secondOrderService;

    @PostMapping(value = "/page", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "订单列表", notes = "订单列表", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public PageResult<OrderDTO> page(@Valid @RequestBody OrderPageRequest request) {
        Page<OrderDTO> page = orderService.orderPageForAdmin(request.getFirstTitle(), request.getPhone(), request.getOrderNo(), request.getProductUuId(), request.getStatus(), request.getStartOrderTime(), request.getEndOrderTime(), request.getStartPayTime(), request.getEndPayTime(), request.getPageNo(), request.getPageSize());
        return PageResult.success(page);
    }

    @PostMapping(value = "/orderExport", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "首发订单导出", notes = "首发订单导出", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public Result orderExport(@Valid @RequestBody OrderExportRequest request) {
        List<OrderSimpleVO> orderSimpleVOList = orderService.orderExport(request.getProductId());
        OrderExportVO orderExportVO = new OrderExportVO();
        orderExportVO.setExcelName("首发订单" + DateUtil.format(DateUtil.date(),"yyyy年MM月dd日") + ".xlsx");
        orderExportVO.setOrderSimpleVOList(orderSimpleVOList);
        return Result.success(orderExportVO);
    }

    @PostMapping(value = "/orderDetailExport", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "首发订单明细导出", notes = "首发订单明细导出", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public Result<OrderDetailExportVO> orderDetailExport(@RequestBody OrderDetailExportRequest request) {
        if (ObjectUtil.isNull(request.getProductId()) && (ObjectUtil.isNull(request.getPayTimeFrom()) || ObjectUtil.isNull(request.getPayTimeFrom()))){
            Result.fail("商品筛选和付款时间筛选不能都为空");
        }
        List<OrderDetailListVO> orderDetailListVOList = orderService.orderDetailExport(request.getProductId(), request.getPayTimeFrom(), request.getPayTimeTo());
        OrderDetailExportVO result = new OrderDetailExportVO();
        result.setExcelName("首发订单明细" + DateUtil.format(DateUtil.date(),"yyyy年MM月dd日") + ".xlsx");
        result.setOrderDetailListVOList(orderDetailListVOList);
        return Result.success(result);
    }


    @PostMapping(value = "/unfreeze", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "解除订单限制", notes = "解除订单限制", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public Result unfreeze(@Valid @RequestBody UnfreezeDTO unfreezeDTO) throws GlobalException {
        secondOrderService.unfreeze(unfreezeDTO.getUserTel());
        return Result.success();
    }


    @PostMapping(value = "/nowDataLockOrderSize", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "获取用户当天被锁数据", notes = "获取用户当天被锁数据", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public Result<AdminSecondOrderNowDataLockVO> nowDataLockOrderSize(@Valid @RequestBody UnfreezeDTO unfreezeDTO) {
        AdminSecondOrderNowDataLockVO adminSecondOrderNowDataLockVO = secondOrderService.nowDataLockOrderSize(unfreezeDTO.getUserTel());
        return Result.success(adminSecondOrderNowDataLockVO);
    }

}
